package com.fsq.portscan.Utils;

import java.net.InetAddress;
import java.net.UnknownHostException;
import java.util.ArrayList;
import java.util.LinkedList;

public class IpUtils {


	public static LinkedList<String> stringToIps(String str) throws UnknownHostException {

		LinkedList<String> ips = new LinkedList<String>();
		String strIps[] = str.split("\n");

		for (String stringa : strIps) {
			String string = stringa.trim();
			if (isIPv4LiteralAddress(string)) { //  192.168.1.12
				ips.add(string);
			} else if (string.lastIndexOf("-") != -1) { // 192.168.1.1-100
				String iph[] = string.split("-");
				if (isIPv4LiteralAddress(iph[0])) {
					String x = iph[0];
					String ipx[] = x.split("\\.");
					Integer start = null;
					Integer end = null;

					if(ipx.length<4){
						ips.add(string);
						continue;
					}

					try {
						start = Integer.parseInt(ipx[3]);
						end = Integer.parseInt(iph[1]);
					} catch (NumberFormatException e) {
						ips.add(string);
						e.printStackTrace();
						continue;
					}

					while (start <= end) {
						String ip = ipx[0] + "." + ipx[1] + "." + ipx[2] + "." + start.toString();
						ips.add(ip);
						start++;
					}
				}

			} else if (string.lastIndexOf("/") != -1) { // 192.168.1.1/24
				String iph[] = string.split("/");
				if (isIPv4LiteralAddress(iph[0])) {
					Integer mask = Integer.parseInt(iph[1]);
					if (mask <= 32 && mask >= 1) {
						ips.addAll(maskToIps(iph[0], mask));
					}

				}
			} else if(!string.equals("")){ //www.baidu.com
				ips.add(string);
			}
		}
		return ips;

	}
	
	public static LinkedList<String> maskToIps(String ip, Integer m) throws UnknownHostException {

		LinkedList<String> i = new LinkedList<String>();

		InetAddress inetAddress = InetAddress.getByName(ip);
		int address = inetAddress.hashCode();
		Integer n = 32 - m;
		int startIp = (address & ((0xFFFFFFFF) << n));
		int endIp = (address | ((0xFFFFFFFF) >>> m));
		startIp++;
		endIp--;

		while (startIp <= endIp) {

			byte[] startaddr = getAddress(startIp);
			InetAddress from = InetAddress.getByAddress(startaddr);
			String fromIp = from.getHostAddress();
			i.add(fromIp);
			startIp++;
		}

		return i;
	}

	
//	public static ArrayList<String> maskToIps(String ip, Integer m) throws UnknownHostException {
//
//		ArrayList<String> i = new ArrayList<String>();
//
//		InetAddress inetAddress = InetAddress.getByName(ip);
//		int address = inetAddress.hashCode();
//		Integer n = 32 - m;
//		int startIp = (address & ((0xFFFFFFFF) << n));
//		int endIp = (address | ((0xFFFFFFFF) >>> m));
//		startIp++;
//		endIp--;
//
//		while (startIp <= endIp) {
//
//			byte[] startaddr = getAddress(startIp);
//			InetAddress from = InetAddress.getByAddress(startaddr);
//			String fromIp = from.getHostAddress();
//			i.add(fromIp);
//			startIp++;
//		}
//
//		return i;
//	}


	public static boolean isIPv4LiteralAddress(String ipString){

		//1、判断是否是7-15位之间（0.0.0.0-255.255.255.255.255）
		if (ipString.length()<7||ipString.length()>15) {
			return false;
		}
		//2、判断是否能以小数点分成四段
		String[] ipArray = ipString.split("\\.");
		if (ipArray.length != 4) {
			return false;
		}
		for (int i = 0; i < ipArray.length; i++) {
			//3、判断每段是否都是数字
			try {
				int number = Integer.parseInt(ipArray[i]);
				//4.判断每段数字是否都在0-255之间
				if (number <0||number>255) {
					return false;
				}
			} catch (Exception e) {
				return false;
			}
		}
		return true;
	}
	
	
	
	
	public static byte[] getAddress(int intIp) {
		int address = intIp;
		byte[] addr = new byte[4];

		addr[0] = (byte) ((address >>> 24) & 0xFF);
		addr[1] = (byte) ((address >>> 16) & 0xFF);
		addr[2] = (byte) ((address >>> 8) & 0xFF);
		addr[3] = (byte) (address & 0xFF);
		return addr;
	}

//	public static ArrayList<Integer> stringToPorts(String str) {
//		String ports[] = str.split(",");
//		
//		ArrayList<Integer> portList = new ArrayList<Integer>();
//		for (String stringa : ports) {
//			String string = stringa.trim();
//			if (string.lastIndexOf("-") != -1) {
//				String strPorts[] = string.split("-");
//				Integer startPort = Integer.parseInt(strPorts[0]);
//				Integer endPort = Integer.parseInt(strPorts[1]);
//				while (startPort <= endPort) {
//
//					if (startPort >= 0 && startPort <= 0xFFFF) {
//						portList.add(startPort);
//						startPort++;
//
//					}
//				}
//			} 
//			else{
//				Integer port = Integer.parseInt(string);
//				if (port >= 0 && port <= 0xFFFF) {
//					portList.add(port);
//				}
//			}
//
//			
//		}
//		return portList;
//
//	}

	
	public static LinkedList<Integer> stringToPorts(String str) {
		String ports[] = str.split(",");
		
		LinkedList<Integer> portList = new LinkedList<Integer>();
		for (String stringa : ports) {
			String string = stringa.trim();
			if (string.lastIndexOf("-") != -1) {
				String strPorts[] = string.split("-");
				Integer startPort = Integer.parseInt(strPorts[0]);
				Integer endPort = Integer.parseInt(strPorts[1]);
				while (startPort <= endPort) {

					if (startPort >= 0 && startPort <= 0xFFFF) {
						portList.add(startPort);
						startPort++;

					}
				}
			} 
			else{
				Integer port = Integer.parseInt(string);
				if (port >= 0 && port <= 0xFFFF) {
					portList.add(port);
				}
			}

			
		}
		return portList;

	}

	
	
	
	
	
	// public static boolean isIps(String str){
	//
	//
	// String rexpIp =
	// "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}";
	// String rexpMask =
	// "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}/";
	// String rexpFromTo =
	// "([1-9]|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])(\\.(\\d|[1-9]\\d|1\\d{2}|2[0-4]\\d|25[0-5])){3}-";
	// return true;
	// }
	// public static boolean isPorts(String str)
	// {
	// ArrayList<Integer> portlist = new ArrayList<Integer>();
	//
	//
	//
	// return true;
	// }

}
